<?php
/**
 * created by zzy
 * date: 2018/1/6 15:49
 */
namespace App\Eloquent\Ygt;
use DB;
class OrderFieldCompany extends DbEloquent
{
    protected $table = 'ygt_order_field_company';

    //取厂的工单第一步字段列表
    public static function getActiveFieldList($companyId){
        OrderField::setFieldList($companyId);
        $where                      = [
            'company_id'=>$companyId, 'status'=>1
        ];
        $obj                        = self::getObject();
        $collection                 = self::setWhere($obj,$where);
        $collection                 = $collection->orderby('status','desc');
        $collection                 = $collection->orderby('orderby','asc');
        $collection                 = $collection->orderby('field_id','asc');
        $collection                 = $collection->get();
        $lists                      = $collection->toArray();

        $fieldIdArr                 = [];
        foreach( $lists as $key=>$val )
        {
            $fieldIdArr[]           = $val['field_id'];
        }
        $result                     = [];
        if($fieldIdArr)
        {
            $where              = ['company_id'=>$companyId,'field_id' => ['in',$fieldIdArr]];
            $fields             = 'id,pid,field_id,title,title_type,is_default';
            $selectCollection   = OrderFieldCompanySelect::getList($where,$fields,'','',['id','asc']);
            $selectList         = $selectCollection->toArray();
            $selectData         = [];
            foreach( $selectList as $key=>$val ){
                $valFieldId                 = $val['field_id'];
                $selectData[$valFieldId][]  = $val;
            }
            foreach( $lists as $key=>$val )
            {
                $valFieldId             = $val['field_id'];
                $valFieldType           = $val['field_type'];
                $arr                    = [
                    'id'=>$valFieldId,'title'=>$val['title'],
                    'field_name'=>$val['field_name'],'field_type'=>$valFieldType,
                    'text_type'=>$val['text_type'],'placeholder'=>$val['placeholder'],
                    'is_must'=>$val['is_must'],'new_page_title'=>$val['title'],
                ];
                if($valFieldType == 14)
                {
                    $arr['field_type']=11;
                }
                //15'宽*长 16宽*长*高
                $placeholderArr     = [];
                if($valFieldType == 15){
                    $placeholderArr         = explode(',',$arr['placeholder'],2);
                    $placeholderArrCount    = count($placeholderArr);
                    if($placeholderArrCount == 0){
                        $placeholderArr     = ['宽','长'];
                    }else if($placeholderArrCount == 1){
                        $placeholderArr[1]  = '长';
                    }
                    if($arr['placeholder'] == '')
                    {
                        $placeholderArr[0]  = '宽';
                    }
                }else if($valFieldType == 16){
                    $placeholderArr         = explode(',',$arr['placeholder'],3);
                    $placeholderArrCount    = count($placeholderArr);
                    if($placeholderArrCount == 0){
                        $placeholderArr     = ['宽','长','M边'];
                    }else if($placeholderArrCount == 1){
                        $placeholderArr[1]  = '长';
                        $placeholderArr[2]  = 'M边';
                    }else if($placeholderArrCount == 2){
                        $placeholderArr[2]  = 'M边';
                    }
                    if($arr['placeholder'] == '')
                    {
                        $placeholderArr[0]  = '宽';
                    }
                }
                $arr['placeholder_arr'] = $placeholderArr;
                $tempArrData            = $tempArr = [];
                $defaultValue           = '';
                if( isset($selectData[$valFieldId]) )
                {
                    $tempArrData         = $selectData[$valFieldId];
                    if($valFieldType == 3)
                    {
                        foreach($tempArrData as $k=>$v)
                        {
                            if($v['is_default']==1)
                            {
                                $defaultValue   = $v['title'];
                            }
                        }
                    }
                }
                $arr['default_value']     = $defaultValue;
                $tempArr                    = Process::setRelationSelectData($tempArrData,$valFieldType);
                $fieldUnit                  = $fieldSelect = [];
                if( $valFieldType == 5 ){
                    $fieldUnit         = $tempArr;
                } else {
                    $fieldSelect       = $tempArr;
                }
                $arr['field_unit']  = $fieldUnit;
                $arr['data']         = $fieldSelect;
                $result[]             = $arr;
            }
        }
        return $result;
    }
    //通过字段名取字段类型
    public static function getFieldTypeByFieldName( $fieldName, $companyId ){

        $where                  = ['field_name'=>$fieldName];
        $where['company_id']   = $companyId;
        $result                 = self::getOneValue( $where, 'field_type' );
        return $result;
    }
}